Install some useful package maintenance scripts to /usr/share/cargo
authorXimin Luo <infinity0@debian.org>
Tue, 25 Jul 2017 11:35:13 +0000 (13:35 +0200)
committerXimin Luo <infinity0@debian.org>
Tue, 25 Jul 2017 11:35:13 +0000 (13:35 +0200)
debian/cargo-checksums-prune.py [deleted file]
debian/install
debian/make_orig_multi.sh
debian/scripts/guess-crate-copyright [new file with mode: 0644]
debian/scripts/prune-checksums [new file with mode: 0755]

diff --git a/debian/cargo-checksums-prune.py b/debian/cargo-checksums-prune.py
deleted file mode 100755 (executable)
index 5589799..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/usr/bin/env python3
-
-# Copyright: 2015 The Debian Project
-# License: MIT-License or Apache-2.0
-#
-# Helper to remove removed-files from .cargo-checksum
-# TODO: rewrite to perl and add to dh-cargo, maybe?
-
-from collections import OrderedDict
-import json
-import os
-import sys
-
-def main(pkgdir):
-  os.chdir(pkgdir)
-  with open(".cargo-checksum.json") as fp:
-    sums = json.load(fp, object_pairs_hook=OrderedDict)
-  
-  oldfiles = sums["files"]
-  newfiles = OrderedDict([entry for entry in oldfiles.items() if os.path.exists(entry[0])])
-  sums["files"] = newfiles
-  
-  if len(oldfiles) > len(newfiles):
-    with open(".cargo-checksum.json", "w") as fp:
-      json.dump(sums, fp, separators=(',', ':'))
-
-if __name__ == "__main__":
-  main(sys.argv[1] if len(sys.argv) > 1 else ".")
index 6286d3c2c30136ec7c9371eb4ab79948cd1da673..5245675046655fca482bbe294370da9777bdea23 100644 (file)
@@ -1 +1,2 @@
-target/*/release/cargo usr/bin
+target/*/release/cargo                  usr/bin
+debian/scripts/*                        usr/share/cargo
index ddd3073a84a79df2bbf4e42a9083cf706a1cf078..785bdc1e60a7e3ef497b520eabd24d43aac2eabe 100755 (executable)
@@ -50,7 +50,7 @@ cargo vendor --explicit-version --verbose
 
 # Clean embedded libs and update checksums
 grep -v '^#' ${VENDOR_FILTER} | xargs  -I% sh -c 'rm -rf vendor/%'
-for i in vendor/*; do ${WORKDIR}/debian/cargo-checksums-prune.py "$i"; done
+for i in vendor/*; do ${WORKDIR}/debian/scripts/prune-checksums "$i"; done
 
 # Report any suspicious files
 cp -R vendor vendor-scan
diff --git a/debian/scripts/guess-crate-copyright b/debian/scripts/guess-crate-copyright
new file mode 100644 (file)
index 0000000..de03593
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/python3
+# Copyright: 2015-2017 The Debian Project
+# License: MIT-License or Apache-2.0
+#
+# Guess the copyright of a cargo crate by looking at its git history.
+
+import datetime
+import pytoml
+import os
+import subprocess
+import sys
+
+this_year = datetime.datetime.now().year
+crates = sys.argv[1:]
+get_initial_commit = len(crates) == 1
+
+for crate in crates:
+       with open(os.path.join(crate, "Cargo.toml")) as fp:
+               data = pytoml.load(fp)
+               repo = data["package"].get("repository", None)
+               if get_initial_commit and repo:
+                       output = subprocess.check_output(
+                               """git clone --bare "%s" tmp.crate-copyright >&2 &&
+cd tmp.crate-copyright &&
+git log --format=%%cI --reverse | head -n1 | cut -b1-4 &&
+git log --format=%%cI           | head -n1 | cut -b1-4 &&
+cd .. &&
+rm -rf tmp.crate-copyright""" % repo, shell=True).decode("utf-8")
+                       first_year, last_year = output.strip().split(maxsplit=2)
+               else:
+                       first_year = "20XX"
+                       last_year = this_year
+               print("""Files: {0}
+Copyright: {1}
+License: {2}
+Comment: see {3}
+""".format(
+               os.path.join(crate, "*"),
+               "\n           ".join("%s-%s %s" % (first_year, last_year, a.replace(" <>", "")) for a in data ["package"]["authors"]),
+               data["package"].get("license", "???").replace("/", " or ").replace("MIT", "Expat"),
+               repo or "???"
+       ))
diff --git a/debian/scripts/prune-checksums b/debian/scripts/prune-checksums
new file mode 100755 (executable)
index 0000000..de7bc8c
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/python3
+# Copyright: 2015-2017 The Debian Project
+# License: MIT-License or Apache-2.0
+#
+# Helper to remove removed-files from .cargo-checksum
+# TODO: rewrite to perl and add to dh-cargo, maybe?
+
+from collections import OrderedDict
+import argparse
+import json
+import os
+import sys
+
+def prune_keep(cfile):
+    with open(cfile) as fp:
+        sums = json.load(fp, object_pairs_hook=OrderedDict)
+
+    oldfiles = sums["files"]
+    newfiles = OrderedDict([entry for entry in oldfiles.items() if os.path.exists(entry[0])])
+    sums["files"] = newfiles
+
+    if len(oldfiles) == len(newfiles):
+        return
+
+    with open(cfile, "w") as fp:
+        json.dump(sums, fp, separators=(',', ':'))
+
+def prune(cfile):
+    with open(cfile, "r+") as fp:
+        sums = json.load(fp)
+        sums["files"] = {}
+        fp.seek(0)
+        json.dump(sums, fp, separators=(',', ':'))
+        fp.truncate()
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser()
+    parser.add_argument("-k", "--keep", action="store_true", help="keep "
+        "checksums of files that still exist, and assume they haven't changed.")
+    parser.add_argument('crates', nargs=argparse.REMAINDER,
+        help="crates whose checksums to prune. (default: ./)")
+    args = parser.parse_args(sys.argv[1:])
+    crates = args.crates or ["."]
+    f = prune_keep if args.keep else prune
+    for c in crates:
+        cfile = os.path.join(c, ".cargo-checksum.json") if os.path.isdir(c) else c
+        f(cfile)